/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.Impl;

import dao.OwnerDao;
import entity.Petowner;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ZZ
 */
public class OwnerDaoImpl extends BaseDao implements OwnerDao {

    @Override
    public void update(int id, int money) {
        try {
            Connection con = BaseDao.getConnection();
            PreparedStatement ppst = con.prepareStatement("update petowner set MONEY=? WHERE ID=" + id);
            ppst.setInt(1, money);
            ppst.executeUpdate();
            ppst.close();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void move(int id) {
        try {
            Connection con = BaseDao.getConnection();
            PreparedStatement ppst = con.prepareStatement("delete from petowner WHERE ID=" + id);
            ppst.executeUpdate();
            ppst.close();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public int add(Petowner owner) {
        int r = 0;
        try {
            Connection con = BaseDao.getConnection();
            PreparedStatement ppst = con.prepareStatement("insert into petowner(ID,NAME,PASSWORD,MONEY) VALUES(?,?,?,?)");
            ppst.setInt(1, owner.getId());
            ppst.setString(2, owner.getName());
            ppst.setString(3, owner.getPassword());
            ppst.setDouble(4, owner.getMoney());
            r = ppst.executeUpdate();
            ppst.close();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(OwnerDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return r;

    }

    @Override
    public List<Petowner> select() {
        Petowner owner;
        List child = new ArrayList();
        try {
            Statement statement = BaseDao.getConnection().createStatement();
            ResultSet rs = statement.executeQuery("select * from petowner");
            while (rs.next()) {
                owner = new Petowner(rs.getInt("ID"),
                        rs.getString("NAME"),
                        rs.getString("PASSWORD"),
                        rs.getDouble("MONEY")
                );

                child.add(owner);

            }
            BaseDao.closeConnection();
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return child;
    }
}
